﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using Capstone_project.DatabaseAccessLayer;
using Capstone_project.BussnessLogicLayer;

namespace Capstone_project
{
    public partial class frmAddNewStaff : Form
    {
        StaffBUS staff = new StaffBUS();

        SqlConnection con;
        DataTable dt;
        SqlDataAdapter adap;
        
        public frmAddNewStaff()
        {
            InitializeComponent();
        }

        private void frmAddNewStaff_Load(object sender, EventArgs e)
        {
            dtpBirthday.MaxDate = DateTime.Today;
            con = Common.con;
            if (con.State != ConnectionState.Open)
                con.Open();
            string sql = "SELECT USER_NAME,FULL_NAME,[ADDRESS], BIRTHDAY, PHONE,(Case GENDER when 'True' then 'Male' when 'False' then 'Female' else 'N/A' end) AS GENDER FROM USER_INFORMATION";
            adap = new SqlDataAdapter(sql, con);
            dt = new DataTable();
            adap.Fill(dt);
            con.Close();
            BindingSource bdsource = new BindingSource();
            bdsource.DataSource = dt;
            dataGridView1.DataSource = bdsource;
            bindingNavigator1.BindingSource = bdsource;

            txtUsername.DataBindings.Add("Text", bdsource, "USER_NAME");
            txtFullname.DataBindings.Add("Text", bdsource, "FULL_NAME");
            txtAddress.DataBindings.Add("Text", bdsource, "ADDRESS");
            dtpBirthday.DataBindings.Add("Value", bdsource, "BIRTHDAY");
            txtPhone.DataBindings.Add("Text", bdsource, "PHONE");
            cbxGender.DataBindings.Add("Text", bdsource, "GENDER");


            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (i % 2 == 0)
                {
                    dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.LightGray;
                }
            }
        }
        private void toolStripSave_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do you want to save changes ?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                try
                {
                    SqlCommandBuilder s = new SqlCommandBuilder(adap);
                    int kq=adap.Update(dt);
                    dataGridView1.DataSource = dt;
                    if (kq != 0)
                    {
                        MessageBox.Show("Changes to be saved");
                        dataGridView1.Refresh();
                    }
                }
                catch
                {
                    MessageBox.Show("Fail to save");
                    dataGridView1.Refresh();
                }
            }
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                SqlCommandBuilder s = new SqlCommandBuilder(adap);
                adap.Update(dt);
                dataGridView1.DataSource = dt;
            }
            catch
            {
                dataGridView1.Refresh();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            txtUsername.Enabled = true;
            txtFullname.Enabled = true;
            txtAddress.Enabled = true;
            dtpBirthday.Enabled = true;
            txtPhone.Enabled = true;
            cbxGender.Enabled = true;
            btnSave.Visible = true;
            btnAdd.Visible = false;
            btnEdit.Enabled = false;
            btnDelete.Enabled = false;
            txtUsername.Text = "";
            txtFullname.Text = "";
            txtAddress.Text = "";
            dtpBirthday.Text = DateTime.Today.ToShortDateString();
            txtPhone.Text = "";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string username = txtUsername.Text;
            string fullname = txtFullname.Text;
            string address = txtAddress.Text;
            DateTime birthday = dtpBirthday.Value;
            string phone = txtPhone.Text;
            string gender = checkgender();
            if (username == "" || txtFullname.Text == "" || txtAddress.Text == "" || txtPhone.Text == "")
            {
                MessageBox.Show("Please enter all information");
            }
            else
            {
                if (Common.CheckUserExist(txtUsername.Text.Trim()) == false)
                {
                    MessageBox.Show("User name is existed, please select another name!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    if(con.State!=ConnectionState.Open)
                    con.Open();
                    SqlCommand cmd = new SqlCommand("CreateNewStaff", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@username", SqlDbType.NVarChar).Value = txtUsername.Text;
                    cmd.Parameters.Add("@fullname", SqlDbType.NVarChar).Value = txtFullname.Text;
                    cmd.Parameters.Add("@address", SqlDbType.NVarChar).Value = txtAddress.Text;
                    cmd.Parameters.Add("@birthday", SqlDbType.NVarChar).Value = dtpBirthday.Value;
                    cmd.Parameters.Add("@phone", SqlDbType.NVarChar).Value = txtPhone.Text;
                    cmd.Parameters.Add("@gender", SqlDbType.NVarChar).Value = gender;
                    int res = cmd.ExecuteNonQuery();
                    con.Close();

                    if (res != 0)
                    {
                        MessageBox.Show("Add success");
                        txtUsername.Enabled = false;
                        txtFullname.Enabled = false;
                        txtAddress.Enabled = false;
                        dtpBirthday.Enabled = false;
                        txtPhone.Enabled = false;
                        cbxGender.Enabled = false;
                        btnSave.Visible = false;
                        btnAdd.Visible = true;
                        btnEdit.Enabled = true;
                        btnDelete.Enabled = true;
                    }
                }
            }
            loadGridview("SELECT USER_NAME,FULL_NAME,ADDRESS,BIRTHDAY,PHONE,GENDER FROM USER_INFORMATION");
            
        }
        public void loadGridview(string sql)
        {
            dt.Rows.Clear();
            SqlConnection con = Common.con;
            if (con.State != ConnectionState.Open)
            con.Open();
            adap = new SqlDataAdapter(sql, con);
            adap.Fill(dt);
            con.Close();
            dataGridView1.DataSource = dt;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            txtFullname.Enabled = true;
            txtAddress.Enabled = true;
            dtpBirthday.Enabled = true;
            txtPhone.Enabled = true;
            cbxGender.Enabled = true;
            btnAdd.Enabled = false;
            btnSaveEdit.Visible = true;
            btnEdit.Visible = false;
            btnDelete.Enabled = false;
        }

        private void btnSaveEdit_Click(object sender, EventArgs e)
        {
            string username = txtUsername.Text;
            string fullname = txtFullname.Text;
            string address = txtAddress.Text;
            DateTime birthday = dtpBirthday.Value;
            string phone = txtPhone.Text;
            string gender = checkgender();
            if (txtFullname.Text == "" || txtAddress.Text == "" || txtPhone.Text == "")
            {
                MessageBox.Show("Please enter all information");
            }
            else
            {
                if(con.State!=ConnectionState.Open)
                    con.Open();
                SqlCommand cmd = new SqlCommand("UpdateStaffInfor", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@username", SqlDbType.NVarChar).Value = txtUsername.Text;
                cmd.Parameters.Add("@fullname", SqlDbType.NVarChar).Value = txtFullname.Text;
                cmd.Parameters.Add("@address", SqlDbType.NVarChar).Value = txtAddress.Text;
                cmd.Parameters.Add("@birthday", SqlDbType.NVarChar).Value = dtpBirthday.Value;
                cmd.Parameters.Add("@phone", SqlDbType.NVarChar).Value = txtPhone.Text;
                cmd.Parameters.Add("@gender", SqlDbType.NVarChar).Value = gender;
                    int res = cmd.ExecuteNonQuery();
                    con.Close();

                    if (res != 0)
                    {
                        MessageBox.Show("Edit success");
                        txtUsername.Enabled = false;
                        txtFullname.Enabled = false;
                        txtAddress.Enabled = false;
                        dtpBirthday.Enabled = false;
                        txtPhone.Enabled = false;
                        cbxGender.Enabled = false;
                        btnSaveEdit.Visible = false;
                        btnEdit.Enabled = true;
                        btnEdit.Visible = true;
                        btnDelete.Enabled = true;
                        dataGridView1.Refresh();
                    }
            }
        }
        public string checkgender()
        {
            string gener;
            if (cbxGender.Text=="Female") gener = "False";
            gener= "True";
            return gener;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (con.State != ConnectionState.Open)
                    con.Open();
                string sql = "DELETE USER_INFORMATION WHERE USER_NAME='" + txtUsername.Text + "'";
                SqlCommand com = new SqlCommand(sql, con);
                int res = com.ExecuteNonQuery();
                con.Close();
                if (res != 0)
                {
                    MessageBox.Show("Delete successful");
                }
            }
            catch { MessageBox.Show("Cannot delete "+txtUsername.Text); }
        }

    }
}
